package nlppatents.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Patent 
{

	public static String firstTerm(String s)
	{
		int index = s.indexOf(' ');
		if(index < 0)
			return s;
		return s.substring(0, index);
	}
	
	public static String lineData(String s)
	{
		int index = s.indexOf(' ');
		if(index < 0)
			return "";
		return s.substring(index+1);
	}
	
	public static Patent readPatent(String filename, boolean incFrequency)
	{
		Patent patent = null;
		
		/*
		 * readState:  determines what thing we are reading
		 * states:
		 *   0 - nothing in particular
		 *   1 - abstract
		 *   2 - claims
		 *   3 - description
		 */
		int readState = 0;
		
		// read our file and make some sentences
		try 
		{
			File file = new File(filename);
			if(file.length() == 0)
				return null;
			
			BufferedReader input = new BufferedReader(new FileReader(filename));
			try 
			{
				String line = null; //not declared within while loop
				String marker = null; // line marker, or the first term of the line; used to determine action
				patent = new Patent();
				Sentence sentence;
				
				while ((line = input.readLine()) != null) 
				{
					if((line = line.trim()).length() > 0)
					{
						switch(readState)
						{
							case 0: // nothing in particular
							{
								// check the first term to determine what this thing is
								marker = firstTerm(line);
								if(marker.equals("number:"))
									patent.number = lineData(line);
								else if(marker.equals("author:"))
									patent.author = lineData(line);
								else if(marker.equals("date:"))
									patent.date = lineData(line);
								else if(marker.equals("title:"))
								{
									patent.title = Sentence.parseRawSentence(lineData(line), incFrequency, patent);
								}
								else if(marker.equals("abstract:"))
								{
									readState = 1;
								}
								else if(marker.equals("related-patent:"))
								{
									patent.relatedPatents.add(lineData(line));
								}
								else if(marker.equals("claims:"))
								{
									readState = 2;
								}
								else if(marker.equals("description:"))
								{
									readState = 3;
								}
								else
								{
									System.err.println("ERROR:  Patent contains invalid line!");
									System.err.println("   " + line);
								}
								break;
							}
							case 1: // abstract
							{
								marker = firstTerm(line);
								if(marker.equals("/abstract"))
								{
									readState = 0;
								}
								else
								{
									sentence = Sentence.parseRawSentence(line, incFrequency, patent);
									if(sentence != null)
									{
										patent.pAbstract.add(sentence);
									}
									else
									{
										System.err.println("ERROR:  Null sentence in ABSTRACT");
									}
								}
								
								break;
							}
							case 2: // claims
							{
								marker = firstTerm(line);
								if(marker.equals("/claims"))
								{
									readState = 0;
								}
								else
								{
									sentence = Sentence.parseRawSentence(line, incFrequency, patent);
									if(sentence != null)
									{
										patent.pClaims.add(sentence);
									}
									else
									{
										System.err.println("ERROR:  Null sentence in CLAIMS");
									}
								}
								
								break;
							}
							case 3: // description
							{
								marker = firstTerm(line);
								if(marker.equals("/description"))
								{
									readState = 0;
								}
								else
								{
									sentence = Sentence.parseRawSentence(line, incFrequency, patent);
									if(sentence != null)
									{
										patent.pDescription.add(sentence);
									}
									else
									{
										System.err.println("ERROR:  Null sentence in DESCRIPTION");
									}
								}
								
								break;
							}
						}
					}
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			patent = null;
		}

		return patent;
	}
	
	public String number;
	public String author;
	public String date;

	public ArrayList<String> relatedPatents;
	
	public Sentence title;
	public ArrayList<Sentence> pAbstract;
	public ArrayList<Sentence> pClaims;
	public ArrayList<Sentence> pDescription;
	
	public Patent()
	{
		number = null;
		author = null;
		date = null;
		relatedPatents = new ArrayList<String>();
		title = null;
		pAbstract = new ArrayList<Sentence>();
		pClaims = new ArrayList<Sentence>();;
		pDescription = new ArrayList<Sentence>();;
	}
	
	public void print()
	{
		// print single terms
		System.out.println("number: " + this.number);
		System.out.println("author: " + this.author);
		System.out.println("date: " + this.date);
		
		// print title
		System.out.println("title: ");
		this.title.printFormatted();
		
		// print related patents
		for(int i = 0; i < this.relatedPatents.size(); i++)
		{
			System.out.println("related-patent: " + this.relatedPatents.get(i));
		}
		
		// print abstract
		System.out.println("abstract:");
		for(int i = 0; i < this.pAbstract.size(); i++)
		{
			this.pAbstract.get(i).printFormatted();
		}
		System.out.println("/abstract");
		
		// print claims
		System.out.println("claims:");
		for(int i = 0; i < this.pClaims.size(); i++)
		{
			this.pClaims.get(i).printFormatted();
		}
		System.out.println("/claims");
		
		// print description
		System.out.println("description:");
		for(int i = 0; i < this.pDescription.size(); i++)
		{
			this.pDescription.get(i).printFormatted();
		}
		System.out.println("/description");
	}
	
	public static void main(String args[])
	{
		POS.init();
		POSWord.init();
		DictWord.init();
		
		Patent p = Patent.readPatent("7000018.txt", true);
		
		if(p != null)
			p.print();
		else
			System.err.println("Reading patent failed!!!");
	}
}
